<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="凭证id" prop="recordId">
        <el-input
          v-model="queryParams.recordId"
          placeholder="请输入凭证id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="凭证时间" prop="recordDate">
        <el-date-picker clearable
          v-model="queryParams.recordDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择凭证时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="科目id" prop="subjectId">
        <el-input
          v-model="queryParams.subjectId"
          placeholder="请输入科目id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科目代码" prop="subjectCode">
        <el-input
          v-model="queryParams.subjectCode"
          placeholder="请输入科目代码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年份和月份" prop="yearAndMonth">
        <el-input
          v-model="queryParams.yearAndMonth"
          placeholder="请输入年份和月份"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账套id" prop="packageId">
        <el-input
          v-model="queryParams.packageId"
          placeholder="请输入账套id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['project:FinRecordVoucherItem:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:FinRecordVoucherItem:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:FinRecordVoucherItem:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['project:FinRecordVoucherItem:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="FinRecordVoucherItemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="凭证id" align="center" prop="recordId" />
      <el-table-column label="凭证号" align="center" prop="recordNo" />
      <el-table-column label="凭证时间" align="center" prop="recordDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.recordDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="借贷方向" align="center" prop="directionType" />
      <el-table-column label="科目id" align="center" prop="subjectId" />
      <el-table-column label="科目代码" align="center" prop="subjectCode" />
      <el-table-column label="科目" align="center" prop="subjectName" />
      <el-table-column label="摘要" align="center" prop="description" />
      <el-table-column label="顺序" align="center" prop="recordSeq" />
      <el-table-column label="借方金额" align="center" prop="jieAmount" />
      <el-table-column label="贷方金额" align="center" prop="daiAmount" />
      <el-table-column label="年份" align="center" prop="yearValue" />
      <el-table-column label="月份" align="center" prop="monthValue" />
      <el-table-column label="年份和月份" align="center" prop="yearAndMonth" />
      <el-table-column label="账套id" align="center" prop="packageId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['project:FinRecordVoucherItem:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['project:FinRecordVoucherItem:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改凭证项对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="FinRecordVoucherItemRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="凭证id" prop="recordId">
          <el-input v-model="form.recordId" placeholder="请输入凭证id" />
        </el-form-item>
        <el-form-item label="凭证时间" prop="recordDate">
          <el-date-picker clearable
            v-model="form.recordDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择凭证时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="科目id" prop="subjectId">
          <el-input v-model="form.subjectId" placeholder="请输入科目id" />
        </el-form-item>
        <el-form-item label="科目代码" prop="subjectCode">
          <el-input v-model="form.subjectCode" placeholder="请输入科目代码" />
        </el-form-item>
        <el-form-item label="科目" prop="subjectName">
          <el-input v-model="form.subjectName" placeholder="请输入科目" />
        </el-form-item>
        <el-form-item label="摘要" prop="description">
          <el-input v-model="form.description" placeholder="请输入摘要" />
        </el-form-item>
        <el-form-item label="借方金额" prop="jieAmount">
          <el-input v-model="form.jieAmount" placeholder="请输入借方金额" />
        </el-form-item>
        <el-form-item label="贷方金额" prop="daiAmount">
          <el-input v-model="form.daiAmount" placeholder="请输入贷方金额" />
        </el-form-item>
        <el-form-item label="年份和月份" prop="yearAndMonth">
          <el-input v-model="form.yearAndMonth" placeholder="请输入年份和月份" />
        </el-form-item>
        <el-form-item label="账套id" prop="packageId">
          <el-input v-model="form.packageId" placeholder="请输入账套id" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="FinRecordVoucherItem">
import { listFinRecordVoucherItem, getFinRecordVoucherItem, delFinRecordVoucherItem, addFinRecordVoucherItem, updateFinRecordVoucherItem } from "@/api/project/FinRecordVoucherItem";

const { proxy } = getCurrentInstance();

const FinRecordVoucherItemList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    recordId: null,
    recordNo: null,
    recordDate: null,
    subjectId: null,
    subjectCode: null,
    yearValue: null,
    monthValue: null,
    yearAndMonth: null,
    packageId: null,
  },
  rules: {
    recordId: [
      { required: true, message: "凭证id不能为空", trigger: "blur" }
    ],
    recordNo: [
      { required: true, message: "凭证号不能为空", trigger: "blur" }
    ],
    recordDate: [
      { required: true, message: "凭证时间不能为空", trigger: "blur" }
    ],
    directionType: [
      { required: true, message: "借贷方向不能为空", trigger: "change" }
    ],
    subjectId: [
      { required: true, message: "科目id不能为空", trigger: "blur" }
    ],
    subjectCode: [
      { required: true, message: "科目代码不能为空", trigger: "blur" }
    ],
    subjectName: [
      { required: true, message: "科目不能为空", trigger: "blur" }
    ],
    recordSeq: [
      { required: true, message: "顺序不能为空", trigger: "blur" }
    ],
    yearValue: [
      { required: true, message: "年份不能为空", trigger: "blur" }
    ],
    monthValue: [
      { required: true, message: "月份不能为空", trigger: "blur" }
    ],
    yearAndMonth: [
      { required: true, message: "年份和月份不能为空", trigger: "blur" }
    ],
    packageId: [
      { required: true, message: "账套id不能为空", trigger: "blur" }
    ],
    createId: [
      { required: true, message: "创建者ID不能为空", trigger: "blur" }
    ],
    updateId: [
      { required: true, message: "更新者ID不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询凭证项列表 */
function getList() {
  loading.value = true;
  listFinRecordVoucherItem(queryParams.value).then(response => {
    FinRecordVoucherItemList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    recordId: null,
    recordNo: null,
    recordDate: null,
    directionType: null,
    subjectId: null,
    subjectCode: null,
    subjectName: null,
    description: null,
    recordSeq: null,
    jieAmount: null,
    daiAmount: null,
    yearValue: null,
    monthValue: null,
    yearAndMonth: null,
    packageId: null,
    createId: null,
    createBy: null,
    createTime: null,
    updateId: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("FinRecordVoucherItemRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加凭证项";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getFinRecordVoucherItem(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改凭证项";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["FinRecordVoucherItemRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateFinRecordVoucherItem(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFinRecordVoucherItem(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除凭证项编号为"' + _ids + '"的数据项？').then(function() {
    return delFinRecordVoucherItem(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('project/FinRecordVoucherItem/export', {
    ...queryParams.value
  }, `FinRecordVoucherItem_${new Date().getTime()}.xlsx`)
}

getList();
</script>
